﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.SMECommon.DAL;
using Buffalo.SMECommon.DAL.Models;
using Buffalo.SMECommon.Enum;
using System.Collections;
using Buffalo.Administration.DAL;
using Buffalo.Administration.DAL.Models;

namespace Buffalo.Web.Areas.SMECommon.Controllers
{
    public class PermissionController : Controller
    {
        private Ext.Net.TreeNodeCollection BuildTree(Ext.Net.TreeNodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.TreeNodeCollection();
            }

            Ext.Net.TreeNode root = new Ext.Net.TreeNode();
            root.Text = "Root";
            nodes.Add(root);

            string prefix = DateTime.Now.Second + "_";
            for (int i = 0; i < 10; i++)
            {
                Ext.Net.TreeNode node = new Ext.Net.TreeNode();
                node.NodeID = i.ToString();
                node.Text = prefix + i;
                root.Nodes.Add(node);
            }

            return nodes;
        }
        public AjaxStoreResult GetListOrgUnitHierachy()
        {
            try
            {
                var query = new PermissionDAL().GetListOrgUnitHierachy(".");
                return new AjaxStoreResult(query, query.Count);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public AjaxStoreResult GetListFeatureRole(int limit, int start, string dir, string sort, string roleName)
        {
            try
            {
                if (string.IsNullOrEmpty(roleName))
                    return new AjaxStoreResult(new List<User>(), 0);

                int total = 0;
                List<FeatureRole> listFeatureRole = new PermissionDAL().GetListFeatureRole(limit, start, dir, sort, out total, roleName);
                return new AjaxStoreResult(listFeatureRole, total);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public AjaxStoreResult GetListFeatureUser(int limit, int start, string dir, string sort, string userName)
        {
            try
            {
                if (string.IsNullOrEmpty(userName))
                    return new AjaxStoreResult(new List<Role>(), 0);

                int total = 0;
                List<FeatureUser> listFeatureUser = new PermissionDAL().GetListFeatureUser(limit, start, dir, sort, out total, userName);
                return new AjaxStoreResult(listFeatureUser, total);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public AjaxStoreResult SaveFeatureRoles()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new PermissionDAL().SaveFeatureRoles(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }

    }
}
